package sorting;

public class InsertionSort {

	static int[] array = new int[] { 5, 9, 4, 0, 14, 6, 14, 99, 3, 44, 17 };

	public static void main(String[] args) {

		sortArray(array);

		printArray(array);

	}

	private static void sortArray(int[] array) {

		for (int i = 1; i < array.length; i++) {
			int j = i;
			while (j - 1 >= 0 && array[j - 1] >= array[i]) {
				j--;
			}

			if (i != j) {
				for (int k=i; k>j ; k--) {
					swap(array, k, k-1);
				}
			}
		}
	}

	private static void swap(int[] array, int i, int j) {
		int tmp = array[i];
		array[i] = array[j];
		array[j] = tmp;

	}

	private static void printArray(int[] array2) {
		for (int a : array) {
			System.out.print(a + " ");
		}
	}

}
